<?php
session_start ();
// 判断用户是否已经登录
if (! isset ( $_SESSION ["userinfo"] ) and empty ( $_SESSION ["userinfo"] ["userid"] )) {
	header ( "location:../index.php" );
	session_unset ();
	session_destroy ();
	exit ();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Language" content="zh-CN">
<link href="./Resource/Style/base.yy.css" rel="stylesheet"
	type="text/css">
<link href="./Resource/Style/info.yy.css" rel="stylesheet"
	type="text/css">
<link href="./Resource/Style/mail_list.yy.css" rel="stylesheet"
	type="text/css">
</head>
<body>
<?php
require_once '../tools/SqlHelper.class.php';
$user_id = $_SESSION ["userinfo"] ["userid"];
$sqlHelper = new SqlHelper ();
if (isset ( $_POST ['delete_submit'] ) and $_POST ['delete_submit'] == '删除') {
	// 获取浏览器发过来的页面数据
	if ($_POST ['select_all_top'] == "on") {
		$sql = "DELETE FROM `message_tb` WHERE `user_receiver_id`='$user_id'";
		$res = $sqlHelper->execute_dql ( $sql );
		if (! ! $res) {
			echo "<script>alert('删除全部最近联系人成功！')</script>";
		} else {
			echo "<script>alert('删除全部最近联系人失败！')</script>";
		}
	} else {
		$id_num = ( int ) $_POST ['id_num'];
		$isDelete = true;
		$haveId = true;
		for($i = 0; $i < $id_num; $i ++) {
			$check_id_num = "check_id_num_" . $i;
			if ($_POST [$check_id_num] == "on") {
				$haveId = false;
				$idattention_tb_id = "attention_tb_id" . $i;
				$idattention_tb = ( int ) $_POST [$idattention_tb_id];
				$sql = "DELETE FROM `message_tb` WHERE `message_id`='$idattention_tb'";
				$res = $sqlHelper->execute_dql ( $sql );
				if (! $res) {
					$isDelete = false;
				}
			}
		}
		if ($haveId) {
			echo "<script>alert('未选择私信！')</script>";
		} else {
			if ($isDelete) {
				echo "<script>alert('删除最近联系人成功！')</script>";
			} else {
				echo "<script>alert('删除最近联系人失败！')</script>";
			}
		}
	}
}
$now_data = getdate ();

$sql = "select count(user_receiver_id) from `message_tb` where `user_receiver_id`='$user_id'";
$res = $sqlHelper->execute_dql ( $sql );
$row = mysql_fetch_array ( $res );
$data_count = $row ['0'];

$sql = "select * from `message_tb` where `user_receiver_id`='$user_id'";
$res = $sqlHelper->execute_dql ( $sql );
?>
	<nav>
		<div class="info_nav">
			<ul class="ul_write">
				<li class="myMailCount pitch"><a href="./MyEail.php">我的私信</a></li>
				<li class="notice myXTCount"><a href="./MyAttention.php">关注</a></li>
				<li class="last"><a class="a_out" href="#">最近联系</a><span
					class="mailCountB"><?php echo $data_count;?></span></li>
				<li class="last"><a href="./SystemInform.php">通知</a></li>
			</ul>
		</div>
	</nav>
	<div class="info_con">
		<form action="./RecentContacts.php" method="post">
			<div class="mail_subnav">
				<span><input class="selectAll" id="select_all_top"
					name="select_all_top" type="checkbox"><label for="select_all_top">全部</label><input
					type="submit" name="delete_submit" value="删除"
					style="width: 50px; height: 20px; margin-top: 10px;"></input></span>
				<p>
					<a class="up up_out" href="#"></a> <a class="down " href="#"></a>
				</p>
			</div>
			<div class="mail_list">
		<?php
		$id_num = 0;
		while ( $row = mysql_fetch_assoc ( $res ) ) {
			$user_send_id = $row ['user_send_id'];
			$sql2 = "select ps_Icon,ps_birthday_year,ps_apartment from `personal_data_tb` where `user_id`='$user_send_id'";
			// 创建一个SqlHelper对象
			$row2 = $sqlHelper->execute_dql ( $sql2 );
			$res2 = mysql_fetch_array ( $row2 );
			$ps_Icon_w = $res2 ['ps_Icon'];
			if ($ps_Icon_w == "0") {
				$ps_Icon_w = "../PersonalPhoto/Images/nohead_new.jpg";
			} else {
				$ps_Icon_w = "../PersonalPhoto/" . $user_send_id . "/" . $ps_Icon_w;
			}
			$sql3 = "select user_name from `user_tb` where `user_id`='$user_send_id'";
			// 创建一个SqlHelper对象
			$row3 = $sqlHelper->execute_dql ( $sql3 );
			$res3 = mysql_fetch_array ( $row3 );
			$user_name_w = $res3 ['user_name'];
			$ps_birthday_year_w = $res2 ['ps_birthday_year'];
			$ps_birthday_year_w = ($now_data ["year"] - ( int ) $ps_birthday_year_w);
			$ps_apartment_w = $res2 ['ps_apartment'];
			$sql4 = "select dd_soliloquy from `detail_data_tb` where `user_id`='$user_send_id'";
			// 创建一个SqlHelper对象
			$row4 = $sqlHelper->execute_dql ( $sql4 );
			$res4 = mysql_fetch_array ( $row4 );
			$user_soliloquy = $res4 ['dd_soliloquy'];
			if ($user_soliloquy == "0") {
				$user_soliloquy = "内心独白未填写";
			}
			echo "<ul class='user_list'>";
			echo "<li class='li01'><input name='check_id_num_" . $id_num . "' type='checkbox' class='showCloseChk' /></li>";
			echo "<a target='new' href='../PersonalSpace/PersonalSpace.php?user_id=" . $row ['user_send_id'] . "'>";
			// echo "<li class='li03' onclick=\"location.href = '../PersonalSpace/PersonalSpace.php?user_id=" . $row ['user_send_id'] . "'\">";
			echo "<li class='li03'>";
			echo "<dl>";
			echo "<dt><img width='37' height='45' src='" . $ps_Icon_w . "'></dt>";
			echo "<dd class='dd_mar'>";
			echo "<font>" . $user_name_w . "</font>";
			echo "<font style='color: black; font-weight: normal;'>" . $ps_birthday_year_w . "岁  " . $ps_apartment_w . "</font>";
			echo "</dd>";
			echo "<dd class='dd_number'>" . $user_soliloquy . "</dd>";
			echo "</dl></li></a>";
			echo "<li class='li04'>";
			echo "<a href='./ReadMessage.php?send_user_id=" . $user_send_id . "&send_type=3' style='background-color: #01CBD3;'>发送私信</a>";
			echo "</li>";
			echo "</li>";
			echo "</ul>";
			echo "<input type='hidden' name='attention_tb_id" . $id_num ++ . "' value='" . $row ['message_id'] . "'></input>";
		}
		$sqlHelper->close_connect ();
		?>
		<input type="hidden" name="id_num" value="<?php echo $id_num;?>"></input>
			</div>
		</form>
	</div>
</body>
</html>